#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 256

void print_arr(int* a, int n) {
    printf("The array is:");
    for (int i = 0; i < n; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
}

int main()
{
    int n;
    printf("输入产生随机数个数:\n");
    scanf("%d", &n);
    if (n>255) {
        printf("请输入一个小于等于255的数。\n");
        return -1;
    }
    FILE* p;
    char s[][7] = {"1.txt", "2.txt", "3.txt"};
    srand(time(NULL)); // 把当前时间作为种子，使得程序每次产生不同的序列
    int tmp = rand() % 3;
    /* printf("%d\n", tmp);
    exit(1); */
    p = fopen(s[tmp], "r");
    // 随机文件已完成
    FILE* f = p;
    int c;
    int a[N] = {0};
    int i = 0;
    int a_length = n+1;
    for (int i = 0; i < a_length; i++) {
        c = fgetc(f);
        a[i] = c - 48;
    }
    // 文件n+1字节读入到数组了
    // 现在取一个自然排列
    int b[N] = {0};
    int b_length = n+1;
    for (int i = 0; i < b_length; i++) {
        b[i] = i;
    }

    // 下面计算j
    int j;
    for (int i = n; i>0; i--) {
        j = (a[i-1]+a[i]) % i;

        b[i] ^= b[j];
        b[j] ^= b[i];
        b[i] ^= b[j];
    }

    print_arr(b, b_length);
    fclose(p);
    return 0;
}
